package com.controller;

import com.dto.BorrowBookDTO;
import com.dto.RecordDTO;
import com.service.LoginServiceImpl;
import com.service.RecordServiceImpl;
import com.util.Result;
import com.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 图书借阅的功能
 * @author linyukun
 * @date 2022/7/19 9:22
 */
@RequestMapping("record")
@RestController
public class RecordController {
    @Autowired
    private LoginServiceImpl loginServiceImpl;

    @Autowired
    private RecordServiceImpl recordServiceImpl;

    /**
     * 借书（管理员）
     * @author linyukun
     * @date 2022-07-12
     */
    @RequestMapping(value="/borrowBook",method = RequestMethod.POST)
    public Result borrowBook(@RequestBody BorrowBookDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess( recordServiceImpl.borrowBook(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 借书（用户）
     * @author linyukun
     * @date 2022-07-12
     */
    @RequestMapping(value="/borrowBookYh",method = RequestMethod.POST)
    public Result borrowBookYh(@RequestBody BorrowBookDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setBorrowUserName(userName);
            return ResultUtil.returnDataSuccess( recordServiceImpl.borrowBook(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取借阅书籍的信息
     * @author linyukun
     * @date 2022-07-12
     */
    @RequestMapping(value="/getRecordByUserName",method = RequestMethod.POST)
    public Result getRecordByUserName(@RequestBody BorrowBookDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess( recordServiceImpl.getRecordByUserName(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 获取借阅书籍的信息列表
     * @author linyukun
     * @date 2022-07-12
     */
    @RequestMapping(value="/getRecordByUserNameList",method = RequestMethod.POST)
    public Result getRecordByUserNameList(@RequestBody BorrowBookDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess( recordServiceImpl.getRecordByUserNameList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取某个人的所有的借阅信息
     * @author linyukun
     * @date 2022-07-12
     */
    @RequestMapping(value="/getAllRecordByUserName",method = RequestMethod.POST)
    public Result getAllRecordByUserName(@RequestBody BorrowBookDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setBorrowUserName(userName);
            return ResultUtil.returnDataSuccess( recordServiceImpl.getAllRecordByUserName(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 归还图书
     * @author linyukun
     * @date 2022-07-12
     */
    @RequestMapping(value="/backBook",method = RequestMethod.POST)
    public Result backBook(@RequestBody BorrowBookDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess( recordServiceImpl.backBook(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 续借书籍
     * @author linyukun
     * @date 2022-07-12
     */
    @RequestMapping(value="/renewBooks",method = RequestMethod.POST)
    public Result renewBooks(@RequestBody List<RecordDTO> recordDTOs){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess( recordServiceImpl.renewBooks(recordDTOs));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取所有的借阅记录
     * @author linyukun
     * @date 2022-07-27
     */
    @RequestMapping(value="/getRecordHistoryList",method = RequestMethod.POST)
    public Result getRecordList(@RequestBody BorrowBookDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            if(!"admin".equals(dto.getIdentity())){
                dto.setBorrowUserName(userName);
            }
            return ResultUtil.returnDataSuccess( recordServiceImpl.getRecordHistoryList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取用户的借阅记录的柱状图
     * @author linyukun
     * @date 2022-07-27
     */
    @RequestMapping(value="/getUserRecordBarData",method = RequestMethod.POST)
    public Result getUserRecordBarData(@RequestBody BorrowBookDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setBorrowUserName(userName);
            return ResultUtil.returnDataSuccess( recordServiceImpl.getUserRecordBarData(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 借阅排行榜
     * @author linyukun
     * @date 2022-10-09
     */
    @RequestMapping(value="/getBorrowRanking",method = RequestMethod.POST)
    public Result getBorrowRanking(@RequestBody BorrowBookDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess( recordServiceImpl.getBorrowRanking(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 邮件通知
     * @author linyukun
     * @date 2022-10-12
     */
    @RequestMapping(value="/emailNotification",method = RequestMethod.POST)
    public Result emailNotification(@RequestBody RecordDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setCreateUser(userName);
            return ResultUtil.returnDataSuccess( recordServiceImpl.emailNotification(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

}
